package DAO;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.sun.org.apache.bcel.internal.generic.NEWARRAY;

import POJO.Photos;
import POJO.Product;
import Util.MySqlDataAccessHelper;

public class PhotoDAO {
	public static Photos getPhotosId(int productId){
		Photos photo=null;
		 MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
	        try {
	            String sql = "select photos.ProductPhotoID,photos.ProductPhotoName " +
	            		"from photos,products " +
	            		"where photos.ProductID=products.ProductID " +
	            		"and products.ProductID="+productId ;
	             	helper.open_en();         
	            ResultSet rs = helper.executeQuery(sql);
	            while (rs.next()) {
	            	photo=new Photos();
	            	photo.setPhotoId(rs.getInt("ProductPhotoID"));
	            	photo.setPhotoName(rs.getString("ProductPhotoName"));	
	            }
	        } catch (Exception e) {
	            System.out.println(e.getMessage());
	        } finally {
	            helper.close();
	        }
	        return photo;
	    }
	
	public static ArrayList<Photos> listPhotoIdProduct(int idPro) {
		ArrayList<Photos> lstPhoto= new ArrayList<Photos>();
		MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
		try {
			String sql ="select photos.ProductPhotoID,photos.ProductPhotoName,photos.ProductID " +
					" from photos,products " +
					" where products.ProductID=photos.ProductID" +
					" and photos.ProductID="+idPro;
		//	System.out.println("sqlMai: "+sql);
			helper.open_en();
			ResultSet rs = helper.executeQuery(sql);
			while (rs.next()) {
			
				Photos photo=new Photos();
				photo.setPhotoId(rs.getInt("ProductPhotoID"));
				photo.setPhotoName(rs.getString("ProductPhotoName"));
				photo.setProductId(rs.getInt("ProductID"));
				lstPhoto.add(photo);		
			}
		} catch (Exception ex) {
			ex.getMessage();
		}
		return lstPhoto;
	}
}
